*****************************************************************
*********** Mining and violent competition in Africa ************
****** A.S. Rigterink, T. Ghani, J.S. Lozano, J.N. Shapiro ******
****** Replication of Tables in section D of the Appendix *******

/*
0. Setting globals
1. Table D1 
2. Tables D2 and D3
3. Table D4 
4. Table D5
5. Tables D6 and D7
6. Table D8 and Figure D1
7. Table D9 
8. Table D10

*/
******************************************************************

clear all
version 17.0
set more off
cap log close

/*--------------------------------
------- 0. Setting globals -------
----------------------------------*/

*** Directory globals
*** NB: SET YOUR DIRECTORY HERE AND UNTEXT IF NOT RUNNING 0-Master.do
*global dir = ".\Replication_data\Stata"
*cd "$dir"

*** Subdirectories
global Do_files     "./Do"
global Data			"./Data"
global Results		"./Results"
global Tables 		"./Tables"
global Figures		"./Figures"

*** Scripts for spatially clustered standard errors
do "$Do_files/my_ols_spatial_HAC.do"
do "$Do_files/my_reg2hdfespatial.ado"

*** Load data
use "$Data/main.dta"

/*----------------------------------
------------ 1. Table D1 -----------
------------------------------------*/

* Condition to stick to same sample
local condition = "price_snl!=. & snl_asm_price!=. & asm_price!=. & snl_asm_asmprice!=. & onlyasm_price!=. & snl_diffasm_asmprice!=. & snl_diffasm_lsmprice!=. & snl_sameasm_price!=."

* Berman-style regression
my_reg2hdfespatial acled_indicator snl_price if `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_SNL_samp", replace

* Reduced sample
my_reg2hdfespatial acled_indicator snl_price if asm_voted!=. & `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_geosample_samp", replace

* LSM*ASM cells get LSM price
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price if `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_samp", replace

* LSM*ASM cells get ASM price
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice if `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_ASMprice_samp", replace

* Same versus different ASM commodity
my_reg2hdfespatial acled_indicator snl_price onlyasm_price snl_diffasm_asmprice snl_diffasm_lsmprice snl_sameasm_price if `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_sameASM_samp", replace

/*------------------------------------------
------------ 2. Tables D2 and D3 -----------
--------------------------------------------*/

* Dropping DRC and Angola
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price if gid_0!="AGO" & gid_0!="COD", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo1c", replace

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice if gid_0!="AGO" & gid_0!="COD", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo1c_a", replace

* Dropping commodity-cells if a single country produces >10%
preserve
drop if gid_0=="AGO" & (snl_commodity=="diamond" | modal_asm=="diamond")
drop if gid_0=="COD" & (snl_commodity=="cobalt" | modal_asm=="2c" | snl_commodity=="coltan" | modal_asm=="3t")

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo1", replace

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo1_a", replace
restore

* Dropping countries that together make up >10% of world production. 
* This drops DRC, Angola, Ghana, Mali, Sudan, South Africa, Botswana, Guinea, Lesotho, Namibia, Sierra Leone, Tanzania, Zimbabwe, Zambia
preserve

drop if gid_0=="COD" | gid_0=="AGO" | gid_0=="GHA" | gid_0=="MLI" | gid_0=="SDN" | gid_0=="ZAF" | gid_0=="BWA" | gid_0=="GIN" | gid_0=="LSO" | gid_0=="NAM" | gid_0=="SLE" | gid_0=="TZA" | gid_0=="ZMB" | gid_0=="ZWE" | gid_0=="BDI" | gid_0=="ETH" | gid_0=="NGA" | gid_0=="RWA"

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo2c", replace

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo2c_a", replace

restore 

* Dropping commodity-cells in these countries

preserve 

drop if (gid_0=="GHA" | gid_0=="MLI" | gid_0=="SDN" | gid_0=="ZAF") & (snl_commodity=="gold" | modal_asm=="gold")
drop if (gid_0=="AGO" | gid_0=="BWA" | gid_0=="COD" | gid_0=="GIN" | gid_0=="LSO" | gid_0=="NAM" | gid_0=="SLE" | gid_0=="TZA" | gid_0=="ZWE" | gid_0=="ZAF") & (snl_commodity=="diamond" | modal_asm=="diamond")
drop if (gid_0=="COD" | gid_0=="ZMB") & (snl_commodity=="copper" | modal_asm=="2c")
drop if gid_0=="COD" & (snl_commodity=="cobalt" | modal_asm=="2c" | snl_commodity=="coltan" | modal_asm=="3t")
drop if (gid_0=="BDI" | gid_0=="ETH" | gid_0=="NGA" | gid_0=="RWA") & (snl_commodity=="coltan" | modal_asm=="3t")

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo2", replace

my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_endo2_a", replace


restore

* Placebo prices 
my_reg2hdfespatial acled_indicator snl_price_plac asm_price_plac snl_asm_price_plac, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_plac", replace

my_reg2hdfespatial acled_indicator snl_price_plac asm_price_plac snl_asm_asmprice_plac, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_plac_a", replace

/*----------------------------------
------------ 3. Table D4 -----------
------------------------------------*/

* Berman-style regression
my_reg2hdfespatial acled_indicator price_snl_w, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_SNL_w", replace

* Reduced sample
my_reg2hdfespatial acled_indicator price_snl_w if asm_voted!=., timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_geosample_w", replace

* LSM*ASM cells get LSM price
my_reg2hdfespatial acled_indicator price_snl_w asm_price_pred_asm_w snl_asm_price_w, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_main_w", replace

* LSM*ASM cells get ASM price
my_reg2hdfespatial acled_indicator price_snl_w asm_price_pred_asm_w snl_asm_price_pred_asm_w, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_main_ASMprice_w", replace

* Same versus different ASM commodity
my_reg2hdfespatial acled_indicator price_snl_w onlyasm_price_w snl_diffasm_asmprice_w snl_diffasm_lsmprice_w snl_sameasm_price_w, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_sameASM_w", replace

/*----------------------------------
------------ 4. Table D5 -----------
------------------------------------*/

* Berman-style regression
my_reg2hdfespatial acled_indicator price_snl_shock, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_SNL_shock", replace

* Reduced sample
my_reg2hdfespatial acled_indicator price_snl_shock if asm_voted!=., timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_geosample_shock", replace

* LSM*ASM cells get LSM price
my_reg2hdfespatial acled_indicator price_snl_shock price_asm_shock snl_asm_price_shock, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_main_shock", replace

* LSM*ASM cells get ASM price
my_reg2hdfespatial acled_indicator price_snl_shock price_asm_shock snl_asm_asmprice_shock, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_main_ASMprice_shock", replace

* Same versus different ASM commodity
my_reg2hdfespatial acled_indicator price_snl_shock onlyasm_price_shock snl_diffasm_asmprice_shock snl_diffasm_lsmprice_shock snl_sameasm_price_shock, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_main_sameASM_shock", replace

/*------------------------------------------
------------ 5. Tables D6 and D7 -----------
--------------------------------------------*/

foreach X in gold asm modal modal_prob {

	my_reg2hdfespatial acled_indicator snl_price asm_s_price_`X' snl_asm_s_price, timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
	estimates save "$Results/T_rob_`X'_s", replace
	
	my_reg2hdfespatial acled_indicator snl_price asm_s_price_`X' snl_asm_s_price_`X', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000) 
	estimates save "$Results/T_rob_`X'_as", replace

}

/*------------------------------------------------
------------ 6. Table D8 and Figure D1 -----------
--------------------------------------------------*/

local condition = "snl_asm_price!=. & asm_price!=. & snl_asm_actual_price!=. & actual_asm_price!=."

** First stage 
my_reg2hdfespatial actual_asm asm_voted if gid_0!="BFA", timevar(year) panelvar(it) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_actual_first", replace

** Main results with restricted sample 
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price if gid_0!="BFA" & `condition', timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_actual_main", replace

** Using false positives and negatives 

* Using LSM prices for triple interaction cells and predicted prices for true positive cells 
my_reg2hdfespatial acled_indicator snl_price asm_truepos_price_voted snl_asm_truepos_price asm_falsepos_price snl_asm_falsepos_price asm_falseneg_price snl_asm_falseneg_price if gid_0!="BFA", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_actual_pos_lsm_1", replace

* Using ASM prices for triple interaction cells and predicted prices for true positive cells 
my_reg2hdfespatial acled_indicator snl_price asm_truepos_price_voted snl_asm_truepos_asmprice_v asm_falsepos_price snl_asm_falsepos_asmprice asm_falseneg_price snl_asm_falseneg_asmprice if gid_0!="BFA", timevar(it) panelvar(cell) lat(y) lon(x) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_actual_pos_asm_1", replace

/*-----------------------------------
------------ 7. Table D9 -----------
-------------------------------------*/

use "$Data/main_largegrid.dta", clear

* Berman-style regression
my_reg2hdfespatial acled_indicator snl_price, timevar(it2) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_large_Berman", replace

* Reduced sample
my_reg2hdfespatial acled_indicator snl_price if asm_voted!=., timevar(it2) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_large_samp", replace

* LSM*ASM cells get LSM price
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_price, timevar(it2) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_large", replace

* LSM*ASM cells get ASM price
my_reg2hdfespatial acled_indicator snl_price asm_price snl_asm_asmprice, timevar(it2) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_large_ASMprice", replace

/*----------------------------------
------------ 8. Table D10 -----------
------------------------------------*/

preserve 

* drop diamonds and tantalum for baseline 
drop if mainmineral == "diamond" | mainmineral == "tantalum"

* Original Berman
my_reg2hdfespatial acled  main_lprice_hist0, timevar(it) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000) 
estimates save "$Results/Table_rob_Berman_or", replace

* Berman reduced sample
my_reg2hdfespatial acled  main_lprice_hist0 if asm_voted!=., timevar(it) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_Berman_samp", replace

* Full interaction model: LSM*ASM is LSM price 
my_reg2hdfespatial acled main_lprice_hist0 asm_price price_hist0_asm, timevar(it) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_Berman", replace

* Full interaction model: LSM*ASM is ASM price 
my_reg2hdfespatial acled main_lprice_hist0 asm_price asmprice_hist0_asm , timevar(it) panelvar(cell) lat(latitude) lon(longitude) distcutoff(500) lagcutoff(100000)
estimates save "$Results/Table_rob_Berman_ASMprice", replace

restore

